import * as THREE from 'three'
import { Utils } from './utils.js'

import TWEEN from '../utils/tween.js'
export const Move = {
    runing(start:any, end:any, call:(start:any) => void, time:number|undefined = 1000, type?:string) {
        return new Promise<any>((resolve, reject) => {
            const t = new TWEEN.Tween(start)
            .to(end, time)
            
            .onUpdate(() => {
                call(start)
            }).onComplete(() => {
                resolve(true)
            }).start()
            if (type!=='none') {
                t.easing((amount) => TWEEN.Easing.Quadratic.Out(amount))
            }
            
        })
    },
    getRy(toPos:THREE.Vector3, playerPos:THREE.Vector3) {
        let a = Math.abs(toPos.x - playerPos.x)
        let b = Math.abs(toPos.z - playerPos.z)
        let c = Math.sqrt(a*a + b*b)

        let result = 0
        let status = ''
        if (toPos.x > playerPos.x && toPos.z < playerPos.z) {
            // 右上
            result = THREE.MathUtils.degToRad(90 - THREE.MathUtils.radToDeg(Math.asin(a/c)) + 90)
            status = '右上'
        } else if (toPos.x > playerPos.x && toPos.z > playerPos.z) {
            // 右下
            result = THREE.MathUtils.degToRad(THREE.MathUtils.radToDeg(Math.asin(a/c)))
            status = '右下'
        } else if (toPos.x < playerPos.x && toPos.z < playerPos.z) {
            // 左上
            result = THREE.MathUtils.degToRad(THREE.MathUtils.radToDeg(Math.asin(a/c)) + 180)
            status = '左上'
        } else if (toPos.x < playerPos.x && toPos.z > playerPos.z) {
            // 左下
            result = THREE.MathUtils.degToRad(90 - THREE.MathUtils.radToDeg(Math.asin(a/c)) + 270)
            status = '左下'
        }
        // console.log(status, THREE.MathUtils.radToDeg(result), a, b, c)
        if (result === 0) {
            if (b === 0) {
                if (toPos.x - playerPos.x < 0) {
                    result = THREE.MathUtils.degToRad(270)
                } else {
                    result = THREE.MathUtils.degToRad(90)
                }
            } else if (a === 0) {
                
                if (toPos.z - playerPos.z < 0) {
                    result = THREE.MathUtils.degToRad(180)
                } else {

                }
            }
        }
        
        return result
    },
    async to(player:THREE.Group | THREE.Object3D, position:THREE.Vector3, onStart:() => Promise<any>, onEnd:() =>Promise<any> ) {
        const y = this.getRy(position, player.position)
        // const rad = Math.abs(THREE.MathUtils.radToDeg(y) - THREE.MathUtils.radToDeg(player.rotation.y))
        // let rTime = rad / (30 / 300)
        await this.runing({
            y: player.rotation.y,
        }, {
            y: y
        }, (start) => {
            player.rotation.y = start.y
        }, 300)
        

        // 移动速度
        let runTime = player.position.distanceTo(position) / (10 / 1000)
        await onStart()
        await this.runing({
            ...player.position
        }, {
            ...position
        }, (start) => {
            player.position.set(start.x, start.y, start.z)
        }, runTime, 'none')
        onEnd()
        
    },
    to1() {

    }
}